package ysq.xyg.util;

import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author yu sheng qiang
 * @create 2017-03-27 15:43
 * 时间和字符串之间转换
 * */
public class DateUtil {
    /** 年格式 */
    public final static String DATE_YEAR = "yyyy";
    /** 月格式 */
    public final static String DATE_MONTH = "MM";
    /** 日格式 */
    public final static String DATE_DAY = "dd";
    /** 日期格式 */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    
    /** 年月格式 */
    public final static String DATE_YEARMONTH = "yyyy-MM";

    /** 时间格式 */
    public final static String TIME_PATTERN = "HH:mm:ss";
    /** 时格式 */
    public final static String DATE_HOUR = "HH";
    /** 时间格式 */
    public final static String HM_PATTERN = "HH:mm";
    /** 日期格式1 */
    public final static String DATE_TIME_PATTERN_1 = "yyyyMMddHHmmss";
    /** 日期格式1 */
    public final static String DATE_TIME_PATTERN_2 = "yyyyMMddHHmmssSSSS";

    /** 日期时间格式 */
    public final static String DATE_TIME_PATTERN = DATE_PATTERN + " " + TIME_PATTERN;

    /**
     * 私有函数，将字符串转换为日期
     * 
     * @param pattern
     *            DATE_PATTERN / TIME_PATTERN / DATE_TIME_PATTERN
     * @param locale
     *            本地化
     * @param zone
     *            时区
     * @param strDate
     *            字符串
     * @return
     * @throws ParseException
     */
    private static final Date convertStringToDate(String pattern, Locale locale, TimeZone zone, String strDate) throws ParseException {
        if(locale == null){
            locale = Locale.getDefault();
        }

        if (zone == null){
            zone = TimeZone.getDefault();
        }

        SimpleDateFormat df = new SimpleDateFormat(pattern, locale);
        df.setTimeZone(zone);
        try {
            return df.parse(strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
    }

    /**
     * 将字符串转换为时：分格式
     * 
     * @param strDate
     * @return
     */
    public static final Date convertStringToHm(String strDate) {
        Locale locale = Locale.CHINESE;
        try {
            return convertStringToDate(HM_PATTERN, locale, null, strDate);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将字符串转换为中国日期格式
     * 
     * @param strDate
     * @return
     */
    public static final Date convertStringToDate(String strDate) {
        Locale locale = Locale.CHINESE;
        try {
            return convertStringToDate(DATE_PATTERN, locale, null, strDate);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将字符串转换为日期时间格式
     * 
     * @param strDate
     * @return
     */
    public static final Date convertStringToDateHMS(String strDate) {
        Date date = null;
        try {
            if (null != strDate && !"".equals(strDate)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                date = sdf.parse(strDate);
            }
        } catch (Exception e) {
            return null;
        }
        return date;
    }
    /**
     * 字符串转换为日期类型
     *
     * @return
     */
    public static final Date strToDate(String pattern,String strData) {
        Locale locale = Locale.CHINESE;
        try {
            return convertStringToDate(pattern, locale, null, strData);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 得到yyyy-MM-01 00:00:00类型的开始时间
     *
     * @return
     */
    public static final Date getBeginMonth(String beginMonth) {
        Locale locale = Locale.CHINESE;
        try {
            return convertStringToDate("yyyy-MM-dd HH:mm:ss", locale, null, beginMonth + "-01 00:00:00");
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 得到yyyy-MM-dd 00:00:00类型的开始时间
     *
     * @param beginDate
     * @return
     */
    public static final Date getBeginDate(String beginDate) {
        Locale locale = Locale.CHINESE;
        try {
            return convertStringToDate("yyyy-MM-dd HH:mm:ss", locale, null, beginDate + " 00:00:00");
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 提供yyyy-MM-dd类型的日期字符串转化，专门提供Web页面结束日期转化 如输入2006-07-27，则转化为 2006-07-28
     * 00:00:00
     * 
     * @param endDate
     * @return
     */
    public static final Date getEndDate(String endDate) {
        Locale locale = Locale.CHINESE;
        try {
            Date date = convertStringToDate("yyyy-MM-dd HH:mm:ss", locale, null, endDate + " 00:00:00");
            return new Date(date.getTime() + 24 * 3600 * 1000);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 提供yyyy-MM-dd类型的日期字符串转化,如输入2006-07-27，则转化为 2006-07-28 23:59:59
     * 
     * @param endDate
     * @return
     */
    public static final Date getAllDate(String endDate) {
        Date date = null;
        try {
            if (null != endDate && !"".equals(endDate)) {
                endDate = endDate + " 23:59:59";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sdf.parse(endDate);
            }
        } catch (Exception e) {
            return null;
        }
        return date;
    }

    /**
     * 私有函数，将日期转换为字符串
     * 
     * @param pattern
     *            DATE_PATTERN / TIME_PATTERN / DATE_TIME_PATTERN
     * @param locale
     *            本地化
     * @param zone
     *            时区
     * @param aDate
     *            日期
     * @return
     */
    private static final String convertDateToString(String pattern, Locale locale, TimeZone zone, Date aDate) {
        if (locale == null)
            locale = Locale.getDefault();
        if (zone == null)
            zone = TimeZone.getDefault();
        SimpleDateFormat df = new SimpleDateFormat(pattern, locale);
        df.setTimeZone(zone);
        try {
            return df.format(aDate);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 将日期转换为中国语言的字符串
     * 
     * @param pattern
     *            DATE_PATTERN / TIME_PATTERN / DATE_TIME_PATTERN
     * @param aDate
     * @return
     */
    public static final String convertDateToString(String pattern, Date aDate) {
        Locale locale = Locale.CHINESE;
        return convertDateToString(pattern, locale, null, aDate);
    }

    /**
     * 例如返回为（包含日期和星期）：2015年8月4日 星期二
     * 
     * @return String = 2015年8月4日 星期二
     */
    public static String getFullDateStr() {
        DateFormat format = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINESE);
        return format.format(new Date());
    }
    /**
     * 计算两个月份之间相差的月数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetweenMonths(String  date1, String date2) {
        int result = 0;
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime start = formatter.parseDateTime(date1);
        DateTime end = formatter.parseDateTime(date2);
        result = Months.monthsBetween(start, end).getMonths();
        return result;
    }
    /**
     * 计算两个日期之间相差的天数
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetweenDates(Date date1, Date date2) {
        int result = 0;

        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();

        gc1.setTime(date1);
        gc2.setTime(date2);
        result = getBetweenDays(gc1, gc2);

        return result;
    }

    /**
     * 计算两个日期之间相差的天数
     * 
     * @param g1
     * @param g2
     * @return
     */
    private static int getBetweenDays(GregorianCalendar g1, GregorianCalendar g2) {
        int elapsed = 0;
        GregorianCalendar gc1, gc2;

        if (g2.after(g1)) {
            gc2 = (GregorianCalendar) g2.clone();
            gc1 = (GregorianCalendar) g1.clone();
        }
        else {
            gc2 = (GregorianCalendar) g1.clone();
            gc1 = (GregorianCalendar) g2.clone();
        }

        gc1.clear(Calendar.MILLISECOND);
        gc1.clear(Calendar.SECOND);
        gc1.clear(Calendar.MINUTE);
        gc1.clear(Calendar.HOUR_OF_DAY);

        gc2.clear(Calendar.MILLISECOND);
        gc2.clear(Calendar.SECOND);
        gc2.clear(Calendar.MINUTE);
        gc2.clear(Calendar.HOUR_OF_DAY);

        while (gc1.before(gc2)) {
            gc1.add(Calendar.DATE, 1);
            elapsed++;
        }
        return elapsed;
    }

    /**
     * 功能：将表示时间的字符串以给定的样式转化为java.util.Date类型
     * 且多于或少于给定的时间多少小时（formatStyle和formatStr样式相同）
     * 
     * @param:formatStyle 要格式化的样式（DATE_TIME_PATTERN）:yyyy-MM-dd HH:mm:ss
     * @param:formatStr 待转化的字符串(表示的是时间)
     * @param:hour 多于或少于的小时数(可正可负) 单位为小时
     * @return java.util.Date
     */
    public static Date formartDate(String formatStyle, String formatStr, int hour) {
        SimpleDateFormat format = new SimpleDateFormat(formatStyle, Locale.CHINA);
        try {
            Date date = new Date();
            date.setTime(format.parse(formatStr).getTime() + hour * 60 * 60 * 1000);
            return date;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取昨天
     * 
     * @return
     */
    public static Date getYesterday() {
        return new Date(new Date().getTime() - 24 * 3600 * 1000L);
    }

    /**
     * 获取day相对的昨天
     * 
     * @param day
     * @return
     */
    public static Date getYesterdayOfDate(Date day) {
        return new Date(day.getTime() - 24 * 3600 * 1000L);
    }

    /**
     * 获取day相对的明天
     * 
     * @param day
     * @return
     */
    public static Date getTomorrowOfDate(Date day) {
        return new Date(day.getTime() + 24 * 3600 * 1000L);
    }

    /**
     * 获取上周
     * 
     * @param day
     * @return
     */
    public static Date getLastWeek(Date day) {
        return new Date(day.getTime() - 7 * 24 * 3600 * 1000L);
    }

    /**
     * 获取下周
     * 
     * @param day
     * @return
     */
    public static Date getNextWeek(Date day) {
        return new Date(day.getTime() + 7 * 24 * 3600 * 1000L);
    }

    /**
     * 获取当前时间的上个月
     * 
     * @return
     */
    public static Date getLastMonth() {
        return getLastMonth(new Date());
    }

    /**
     * 获取指定时间的上个月
     * 
     * @param date
     * @return
     */
    public static Date getLastMonth(Date date) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.clear();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        cal.getTime();
        return cal.getTime();
    }

    /**
     * 获取指定时间的下个月
     * 
     * @param date
     * @return
     */
    public static Date getNextMonth(Date date) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.clear();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        cal.getTime();
        return cal.getTime();
    }

    /**
     * 获取指定年和月中该月的最大天数
     * 
     * @param year
     *            指定年
     * @param month
     *            指定月 1-12
     * @return 该月最大天数
     */
    public static int getMaxDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.clear();
        cal.set(year, month - 1, 1);
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据指定的年份和指定的第多少周序号得到该周的第一天和最后一天日期
     * 
     * @param year
     *            指定的年，如：2006
     * @param weekNo
     *            指定年份中的第多少周,如：37
     * @return 该周的起始日期和该周的结束日期 Date[0] 起始日期，Date[1] 结束日期
     */
    public static Date[] getGivenWeekDates(int year, int weekNo) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        Date begin = cal.getTime();
        cal.add(Calendar.DAY_OF_YEAR, 6);
        Date end = cal.getTime();
        return new Date[] { begin, end };
    }

    /**
     * 获取指定时间的年份
     * 
     * @param date
     *            指定时间,为null默认为当前日期
     * @return 年份
     */
    public static int getYear(Date date) {
        if (date == null)
            date = new Date();
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.clear();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 得到当前日期的详细信息 int[0]=年份 int[1]=月份 int[2]=天 int[3]=小时 int[4]=分钟 int[5]=秒
     * 
     * @return int[]
     */
    public static int[] getCurrentDateInfo() {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        cal.setTime(new Date());
        int[] date = new int[6];
        date[0] = cal.get(Calendar.YEAR);
        date[1] = cal.get(Calendar.MONTH) + 1;
        date[2] = cal.get(Calendar.DATE);
        date[3] = cal.get(Calendar.HOUR_OF_DAY);
        date[4] = cal.get(Calendar.MINUTE);
        date[5] = cal.get(Calendar.SECOND);
        return date;
    }
    public static Date getCurrentDate() {

        return new Date();
    }
    /**
     * 设置制定的年份和月份，再得到该日期的前多少月或后多少月的日期年份和月�?
     * 
     * @param year
     *            指定的年份，�? 2006
     * @param month
     *            制定的月份，�? 6
     * @param monthSect
     *            月份的差�? 如：现在�?2006�?5月份，要得到�?4月，则monthSect = 4，正确日期结果为2006�?9�?
     *            如：现在�?2006�?5月份，要得到�?4月，则monthSect = -4，正确日期结果为2006�?1�?
     *            如：monthSect = 0，则表示为year年month�?
     * @return int[] int[0] 年份 int[1] 月份
     */
    public static int[] getLimitMonthDate(int year, int month, int monthSect) {
        year = year < 1 ? 1 : year;
        month = month > 12 ? 12 : month;
        month = month < 1 ? 1 : month;
        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), new Locale("zh", "CN"));
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.add(Calendar.MONTH, monthSect);
        int[] yAndM = new int[2];
        yAndM[0] = cal.get(Calendar.YEAR);
        yAndM[1] = cal.get(Calendar.MONTH);
        if (yAndM[1] == 0) {
            yAndM[0] = yAndM[0] - 1;
            yAndM[1] = 12;
        }
        return yAndM;
    }

    /**
     * 取得当前日期是多少周 注意："2015-1-1" 和 "2014-12-31" 都是第52周，都返回 52.实际情况也确实如此。
     * 
     * @param date
     *            指定的日期,为null默认为当前日期
     * @return 当年的第多少周序号，例如：37
     */
    public static int getWeekOfYear(Date date) {

        if (date == null)
            date = new Date();

        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 得到某一年一共有多少周
     * 
     * @param year
     *            年份
     * @return 周总数
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    /**
     * 得到某年某周的第一天
     * 
     * @param year
     *            eg:2014
     * @param week
     *            eg:5
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 得到某年某周的最后一天
     * 
     * @param year
     *            eg:2014
     * @param week
     *            eg:5
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得指定日期所在周的第一天
     * 
     * @param date
     *            指定的日期,,为null默认为当前日期
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {

        if (date == null)
            date = new Date();

        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 取得指定日期所在周的最后一天
     * 
     * @param date
     *            指定的日期,,为null默认为当前日期
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {

        if (date == null)
            date = new Date();

        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 得到本月最后一天
     * 
     * @return
     */
    public static Date getLastDateOfMonth() {
        Date dt = new Date();
        if (dt == null)
            return null;

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR, 12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    /**
     * 得到指定月份最后一天
     *
     * @return
     */
    public static Date getLastDateOfMonth(Date date) {
        if (date == null)
            return null;

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR, 12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    /**
     * 得到指定月份第一天
     *
     * @return
     */
    public static Date getFristDateOfMonth(Date date) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR, -12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    
    /**
     * 得到本月第一天
     * 
     * @return
     */
    public static Date getFristDateOfMonth() {
        Date dt = new Date();
        if (dt == null)
            return null;
        
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR, -12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 得到上月最后一天
     * 
     * @return
     */
    public static Date getLastDateOfLastMonth() {
        Date dt = new Date();

        if (dt == null)
            return null;
        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();
        // 取得系统当前时间所在月第一天时间对象
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 日期减一,取得上月最后一天时间对象
        cal.add(Calendar.DAY_OF_MONTH, -1);
        // 输出上月最后一天日期
        cal.set(Calendar.HOUR, 12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        // System.out.println("上月第一天" + cal.getTime());
        return cal.getTime();
    }

    /**
     * 得到上月第一天
     * 
     * @return
     */
    public static Date getFristDateOfLastMonth() {
        Date dt = new Date();

        if (dt == null)
            return null;
        // 取得系统当前时间
        Calendar cal = Calendar.getInstance();
        // 取得系统当前时间所在月第一天时间对象
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 日期减一,取得上月最后一天时间对象
        cal.add(Calendar.DAY_OF_MONTH, -1);
        // 输出上月最后一天日期
        int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, days);
        cal.set(Calendar.HOUR, -12);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        // System.out.println("上月第一天" + cal.getTime());
        return cal.getTime();

    }

    /**
     * 获取两时间差的详细信息， eg：dateDiff("2015-7-10 3:20:20","2015-7-10 13:20:20",
     * DATE_TIME_PATTERN="yyyy-MM-dd HH:mm:ss")
     * 
     * @param startTime
     * @param endTime
     * @param format
     *            DATE_TIME_PATTERN
     * @return long[天、小时、分钟、秒]
     * @throws ParseException
     */
    public static long[] getDateDiff(String startTime, String endTime, String format) throws ParseException {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        // 获得两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        long day = diff / nd;// 计算差多少天
        long hour = (day * 24) + (diff % nd / nh);// 计算差多少小时
        long min = (hour * 60) + (diff % nd % nh / nm);// 计算差多少分钟
        long sec = (min * 60) + (diff % nd % nh % nm / ns);// 计算差多少秒//输出结果
        long[] i = { day, hour, min, sec };
        return i;
    }

    /**
     * 格式化时间，返回时和分
     * 
     * @param date
     * @return
     */
    public static String getStandardMonthString(Date date) {
        if (date == null) {
            return null;
        }

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String strDate = format.format(date);
        return strDate;
    }

    /**
     * 获取date时间t加上分钟min后的时间
     * 
     * @param t
     * @param min
     * @return
     */
    public static Date getTime(Date t, Integer min) {
        if (null == t || null == min) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(t);
        cal.add(Calendar.MINUTE, min);
        return cal.getTime();
    }

    /**
     * 格式化输出date yyyy-MM-dd hh:mm:ss
     * 
     * @param date
     * @return
     */
    public static String formatFromDate(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 格式化输出date yyyy-MM-dd hh:mm
     * 
     * @param date
     * @return
     */
    public static String formatFromDateHm(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    /**
     * 格式化输出date hh:mm
     * 
     * @param date
     * @return
     */
    public static String formatFromDateToHm(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }

    /**
     * 格式化输出date yyyy-MM-dd hh:mm
     * 
     * @param date
     * @return
     */
    public static String formatFromStringHm(String date) {
        if (date == null) {
            return null;
        }
        String time = null;
        String[] d = date.split(":");
        if (d != null) {
            time = d[0] + ":" + d[1];
        }
        return time;
    }

    /**
     * 时间转为UNIX cron expressions 转换算法 Spring动态计划使用
     * 
     * @param date
     * @return
     */
    public static String convertDateToCronExp(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTime(date);
        cal.getTime();
        int year = cal.get(Calendar.YEAR);// 年份
        int month = cal.get(Calendar.MONTH) + 1;// 月份
        int day = cal.get(Calendar.DATE);// 日
        int hour = cal.get(Calendar.HOUR_OF_DAY);// 小时
        int minute = cal.get(Calendar.MINUTE);// 分
        int second = cal.get(Calendar.SECOND);// 秒
        String returnString = second + " " + minute + " " + hour + " " + day + " " + month + " ? " + year;
        return returnString;
    }

    /**
     * 得到本周周一
     * 
     * @return yyyy-MM-dd
     */
    public static Date getMondayOfThisWeek(int before) {
        Calendar c = Calendar.getInstance();
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        c.add(Calendar.DATE, -dayofweek + 1 - before);
        c.set(Calendar.HOUR, -12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        // System.out.println("本周周一" + c.getTime());
        return c.getTime();
    }

    /**
     * 得到本周周日
     * 
     * @return yyyy-MM-dd
     */
    public static Date getSundayOfThisWeek(int before) {
        Calendar c = Calendar.getInstance();
        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayofweek == 0)
            dayofweek = 7;
        c.add(Calendar.DATE, -dayofweek + 7 - before);
        c.set(Calendar.HOUR, 12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        // System.out.println("本周周日" + c.getTime());
        return c.getTime();
    }

    /**
     * 得到日期的前几天
     * 
     * @param days
     * @param date
     * @return
     */
    public static Date dateBefore(int days, Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }
    
    /**
     * 判断给定日期是否为同一天
     * 描述   
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                        .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    public static Date getDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        try {
            return format.parse(format.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 
     * 计算两个日期相差的月份数
     * 
     * @param date1 日期1
     * @param date2 日期2
     * @return  相差的月份数
     * @throws ParseException
     */
    public static int countMonths(Date date1,Date date2) throws ParseException{
        int year =0;
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        
        //开始日期若小月结束日期
        if(year<0){
            year=-year;
            return year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);
        }
        return year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
    }
    
    /**
     * 获取字符串的年月切转化成integer
     * 2016-06-09
     * 获取201606
     * @return
     * @throws ParseException
     */
    public static Integer getYearMonth(String time) throws ParseException{
    	Locale locale = Locale.CHINESE;
        Date date = convertStringToDate(DATE_YEARMONTH, locale, null, time);
        Calendar c=Calendar.getInstance();
        SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
        String year = sdfYear.format(date);
        String month = sdfMonth.format(date);
        
        Integer yearMonth = Integer.parseInt(year+month);
        return yearMonth;
    }
    
    public static int switchWeek(int weekDay){
    	switch (weekDay) {
		case 1:
			return 7;
		case 2:
			return 1;
		case 3:
			return 2;
		case 4:
			return 3;
		case 5:
			return 4;
		case 6:
			return 5;
		case 7:
			return 6;
		default:
			return 0;
		}
    }
    public static void main(String[] args) throws ParseException {
//        System.out.println(getFullDateStr());
//        System.out.println(getYesterday());
       System.out.println(getMinuteOneDate(new Date(),10));
//
//        System.out.println(getWeekOfYear(convertStringToDate("2015-1-1")));
//        System.out.println(getWeekOfYear(convertStringToDate("2014-12-31")));
//        System.out.println(getWeekOfYear(new Date()));
        /*Date lastDate=getLastDateOfMonth(new Date());
        Date fristDate=getFristDateOfMonth(new Date());
        System.out.println(DateToStr(lastDate,DATE_TIME_PATTERN));
        System.out.println(DateToStr(fristDate,DATE_TIME_PATTERN));*/
        Date n=new Date();
        System.out.print(DateToStr(n,DATE_TIME_PATTERN));
        System.out.print(DateToStr(DateUtil.getSecondOneDate(n,60*60),DATE_TIME_PATTERN));
        //System.out.println(DateToStr(getDateOfDate(new Date(),-1),DATE_TIME_PATTERN));
    }
    /**
     * 字符串转换为Date
     * */
    public static String StrToDate(String date,String pattern){
        if(date == null){
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String strDate = format.format(date);
        return strDate;
    }
    /**
     * date转换为字符串
     * */
    public static String DateToStr(Date date,String pattern){
        if(date == null){
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String strDate = format.format(date);
        return strDate;
    }

    /**
     * 改变Date日期中年
     * */
    public static Date getYearOfDates(Date p_date ,int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(p_date);
        c.add(c.YEAR,-i);
        return c.getTime();
    }
    /**
     * 改变Date日期中月
     * */
    public static Date getMonthOfDate(Date p_date ,int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(p_date);
        c.add(c.MONTH,-i);
        return c.getTime();
    }
    /**
     * 改变Date日期中天
     * */
    public static Date getDateOfDate(Date p_date ,int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(p_date);
        c.add(c.DATE,-i);
        return c.getTime();
    }
    /**
     * 改变Date日期中分钟
     * */
    public static Date getMinuteOneDate(Date p_date ,int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(p_date);
        c.add(c.MINUTE,i);
        return c.getTime();
    }
    /**
     * 改变Date日期中秒
     * */
    public static Date getSecondOneDate(Date p_date ,int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(p_date);
        c.add(c.SECOND,i);
        return c.getTime();
    }

    /**
     * 字符串获取日期
     * @throws ParseException
     */
    public static Date getTextDate(String dateText ,String format){
        try {
            return new SimpleDateFormat(format).parse(dateText);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 设置时间戳
     * */
    public static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
    /**
     * 获取毫秒
     * */
    public static Long accept_timestamp() {
        return System.currentTimeMillis();
    }


    public static int calLastedTime(Date startDate,Date endDate) {
        long a = endDate.getTime();
        long b = startDate.getTime();
        int c = (int)((a - b) / 1000);
        return c;
    }

    public static int calLastedTime(Date startDate,Date endDate, Long time) {
        long a = endDate.getTime();
        long b = startDate.getTime() + time;
        int c = (int)((b - a) / 1000);
        return c;
    }
}
